package cn.sciento.dd.service.impl;

import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import cn.sciento.dd.dto.DefaultResultDTO;
import cn.sciento.dd.dto.GetMicroappListDTO;
import cn.sciento.dd.dto.GetMicroappVisibleDTO;
import cn.sciento.dd.dto.SetMicroappVisibleDTO;
import cn.sciento.dd.service.DingCorpAgentManageService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class DingCorpAgentManageServiceImp implements DingCorpAgentManageService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public GetMicroappListDTO getAllMicroappList(String accessToken) {
    return (GetMicroappListDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/microapp/list?access_token=" + accessToken, new HttpEntity((MultiValueMap)buildHttpHeaders()), GetMicroappListDTO.class);
  }
  
  public GetMicroappListDTO getMicroappListByUserId(String accessToken, String userid) {
    return (GetMicroappListDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/microapp/list_by_userid?access_token=" + accessToken + "&userid" + userid, GetMicroappListDTO.class);
  }
  
  public GetMicroappVisibleDTO getMicroappVisible(String accessToken, Long agentId) {
    return (GetMicroappVisibleDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/microapp/visible_scopes?access_token=" + accessToken, new HttpEntity("{ \"agentId\": " + JSON.toJSONString(agentId) + " }", (MultiValueMap)buildHttpHeaders()), GetMicroappVisibleDTO.class);
  }
  
  public DefaultResultDTO setMicroappVisible(String accessToken, SetMicroappVisibleDTO setMicroappVisibleDTO) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/microapp/set_visible_scopes?access_token=" + accessToken, new HttpEntity(setMicroappVisibleDTO, (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  protected HttpHeaders buildHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    return httpHeaders;
  }
}
